<template>
  <section class="todoapp">
    <!-- 使用组件 -->
    <todo-header @create="createFn" :list="list"></todo-header>
    <todo-main :list="showArr" @del="deleteFn"></todo-main>
    <todo-footer
      :list="showArr"
      @changeType="typeFn"
      @clearDone="clearF"
    ></todo-footer>
  </section>
</template>

<script>
// 1 样式引入
import "./styles/base.css";
import "./styles/index.css";
// 引入组件
import TodoHeader from "./components/TodoHeader.vue";
import TodoMain from "./components/TodoMain.vue";
import TodoFooter from "./components/TodoFooter.vue";
export default {
  components: { TodoHeader, TodoMain, TodoFooter },
  // 铺设代办任务
  data() {
    return {
      // 初始数据
      list: JSON.parse(localStorage.getItem("todoList")) || [],
      getSel: "all",
    };
  },
  methods: {
    createFn(task) {
      this.list.push({
        id:
          this.list.length === 0 ? 100 : this.list[this.list.length - 1].id + 1,
        name: task,
        isDone: false,
      });
    },
    deleteFn(theId) {
      let index = this.list.findIndex((obj) => obj.id === theId);
      this.list.splice(index, 1);
    },
    typeFn(str) {
      this.getSel = str;
    },
    clearF() {
      this.list = this.list.filter((obj) => obj.isDone === false);
    },
  },
  computed: {
    showArr() {
      if (this.getSel === "done") {
        return this.list.filter((obj) => obj.isDone === true);
      } else if (this.getSel === "none") {
        return this.list.filter((obj) => obj.isDone === false);
      } else {
        return this.list;
      }
    },
  },
  watch: {
    list: {
      deep: true,
      handler() {
        localStorage.setItem("todoList", JSON.stringify(this.list));
      },
    },
  },
};
</script>